﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RentAWorker.Business.Interfaces;
using RentAWorker.Web.Factory;
using RentAWorker.DataAccess.Entities;
using RentAWorker.Business.DTO;

namespace RentAWorker.WebNew.CustomerCode
{
    public class SearchResultsHelper
    {
        public const string SearchNameParameter = "searchName";
        public const string LocationParameter = "location";
        public const string FromParameter = "from";
        public const string ToParameter = "to";
        public const string MachineStatusParameter = "status";
        public const string MachineTypeParameter = "machineType";

        private IRentRequestService RentRequestService;

        public SearchResultsHelper()
        {
            RentRequestService = ClientProxyFactory.GetRentRequestProxy();
        }


        public List<Worker> SearchWorkers(string searchName, int location, DateTime from, DateTime to)
        {
            SearchResourceResponse Response = SearchInternal(searchName, location, from, to, null, -1);
            return new List<Worker>(Response.PersonResource);
        }

        public List<Machine> SearchMachines(string searchName, int location, DateTime from, DateTime to, string status, int machineType)
        {
            SearchResourceResponse Response = SearchInternal(searchName, location, from, to, status, machineType);
            return new List<Machine>(Response.MachineResources);
        }

        private SearchResourceResponse SearchInternal(string searchName, int location, DateTime from, DateTime to, string status, int machineType)
        {
            SearchResourceRequest Request = new SearchResourceRequest()
            {
                Name = searchName,
                Location = location,
                FromDate = from,
                ToDate = to,
                MachineType = machineType,
                State = status
            };

            if (Request.FromDate == DateTime.MinValue && Request.ToDate == DateTime.MinValue)
                Request.ToDate = DateTime.MaxValue;

            SearchResourceResponse Response = RentRequestService.SearchAvailableResources(Request);
            return Response;
        }
    }
}
